<script >
import {Lock,UserFilled} from "@element-plus/icons-vue";
import request from "../utils/request";
import {ElMessage} from "element-plus";

export default{
  name:"Register",
  computed: {
    UserFilled() {
      return UserFilled
    },
   Lock(){
      return Lock
   },
  },
  data(){
    return {
      form: {
        username:"",
        password:"",
        checkword:""
      },
      rules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        checkword:[
          { required: true, message: '请输入验证密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods:{
    Register(){
      this.$refs['form'].validate((valid) => {
        if(valid){
      if(this.form.password !== this.form.checkword){
        ElMessage.error('两次密码输入不一致');
        return;
      }
      request.post('/add',this.form).then(res=>{
        if (res.code === 0) {
          ElMessage.success('注册成功');
          this.$router.push('/login');
        } else {
          ElMessage.error(res.msg);
        }
      })
    }
  })}
}
}

</script>

<template>
<div style="width:100%; height:100vh;background-color:darkslateblue; overflow: hidden">
  <div style="width: 400px;margin:200px auto">
    <div style="color:#cccccc; font-size:30px; text-align:center; padding:30px 0">开始注册</div>
    <el-form ref="form" :model="form" size="default" :rules="rules">
      <el-form-item prop="username">
        <el-input v-model="form.username" :prefix-icon="UserFilled " placeholder="请输入用户名"/>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" :prefix-icon="Lock" type="password" placeholder="请输入密码" show-password/>
      </el-form-item>
      <el-form-item prop="checkword">
      <el-input v-model="form.checkword" :prefix-icon="Lock" type="password" placeholder="请再次输入密码" show-password/>
    </el-form-item>
      <el-form-item >
        <el-button style="width:100%" type="primary" @click="Register">注册</el-button>
      </el-form-item>
      <el-form-item >
        <el-button style="width:100%" type="primary" @click="this.$router.push('/login')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<style scoped>

</style>
